<?php
$path = "../uploads/";

$extArr = array("jpg", "png", "gif");

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
	$name = $_FILES['photoimg']['name'];
	$size = $_FILES['photoimg']['size'];
	
	if(empty($name)){
		echo '请选择要上传的图片';
		exit;
	}
	$ext = extend($name);
	if(!in_array($ext,$extArr)){
		echo '图片格式错误！';
		exit;
	}
	if($size>(200*1024)){
		echo '图片大小不能超过200KB';
		exit;
	}

    $imgArr = getimagesize($_FILES['photoimg']["tmp_name"]);
    $height = 190*$imgArr[1]/$imgArr[0];
    $title = $_POST['imgTit'];
	$image_name = time().rand(100,999).".".$ext;
	$tmp = $_FILES['photoimg']['tmp_name'];

    $mysqli  = new  mysqli ( "localhost" ,  "root" ,  "123456" ,  "my_db" );
    $mysqli -> query ("set names 'utf8'");
    $mysqli -> query ( "INSERT INTO imgs (url, height, title) VALUES('$path$image_name','$height','$title')" );
    $mysqli -> close ();

	if(move_uploaded_file($tmp, $path.$image_name)){
		echo '<img src="'.$path.$image_name.'"  class="preview"><p>'.$title.'</p>';
	}else{
		echo '上传出错了！';
	}
	exit;
}
exit;

function extend($file_name){
	$extend = pathinfo($file_name);
	$extend = strtolower($extend["extension"]);
	return $extend;
}